package com.hellpro.lolbox.home.presenter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.bean.VideoBean;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.model.OnLoadListListener;
import com.hellpro.lolbox.home.model.VideoModel;
import com.hellpro.lolbox.home.view.IHomeView;
import com.hellpro.lolbox.home.xiaosmein.XiaoShuoMeiNvWeb;


/**
 * Package_Name: com.hellpro.lolbox.home.presenter
 * Description :
 * author: HellPermanent
 * date:  2016/10/20 2:59
 * remarks：
 */
public class HomeVideoModel implements LoadHoneVideData {
    private  IHomeView mHomeView;
    private final VideoModel videoModel;
private  Context context;
    private VideoBean data;


    public  HomeVideoModel(final Context context, IHomeView view, PullToRefreshListView pullToRefreshListView){
        videoModel = new VideoModel(context);
        this.context =context;
       mHomeView =view;
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String destUrl = data.getData().get(position).getDestUrl();
                Intent intent = new Intent(context, XiaoShuoMeiNvWeb.class);
                intent.putExtra("path",destUrl);
                context.startActivity(intent);


            }
        });
     }
    private Handler mHandler = new Handler(){


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                data = (VideoBean) msg.obj;

                mHomeView.setListViewAdapter(data);

               mHomeView.hideProgress();
            }else if (msg.what==2){
                //失败
                mHomeView.hideProgress();
            }
        }
    };

    @Override
    public void VideoData( int page) {
        String url = UrlConfigs.HOME_VIDEO + page;
        videoModel.loadVideoJson(url, new OnLoadListListener() {
            @Override
            public void onSuccess(VideoBean beanData) {
                if (beanData!=null) {
                    mHomeView.hideProgress();
                    Message obtain = Message.obtain();
                    obtain.what=1;
                    obtain.obj =beanData;
                    mHandler.sendMessage(obtain);

                }
            }

            @Override
            public void onFailure(String msg) {
                mHandler.sendEmptyMessage(2);

            }
        });
    }
}
